<?php if (!defined('ITPK')) exit('You can not directly access the file.');

/**
 * 机器人插件操作类
 * @author 冬天的秘密
 * @link http://bbs.itpk.cn
 * @version 1.0
 */

class WebpluginPer extends WebDBConnection {

	/**
	 * 获取插件
	 * @param int $is_able 是否启用
	 * @param int $is_monitor_all_msg	是否监控所有消息
	 * @return array | boolean
	 */
	public function getMe($is_able = 1, $is_monitor_all_msg = 0) {
		return $this->db->executeQuery("SELECT * FROM web_plugin WHERE is_able = {$is_able} AND is_monitor_all_msg = {$is_monitor_all_msg} ORDER BY id ASC", true, true);
	}

	/**
	 * 根据插件ID取插件信息
	 * @param int $id
	 * @return Ambigous <multitype:, boolean>
	 */
	public function getMeById($id) {
		return $this->db->executeQuery("SELECT * FROM web_plugin WHERE id = {$id} LIMIT 1", true);
	}

	/**
	 * 根据插件类名获取插件信息
	 * @param string $class_name
	 * @return Ambigous <multitype:, boolean>
	 */
	public function getMeByClassname($class_name) {
		return $this->db->executeQuery("SELECT * FROM web_plugin WHERE class_name = '{$class_name}' LIMIT 1", true);
	}

	/**
	 * 获取所有的插件
	 * @return array | boolean
	 */
	public function getMeAll() {
		return $this->db->executeQuery("SELECT * FROM web_plugin ORDER BY id ASC", true, true);
	}

	/**
	 * 卸载插件
	 * @param int $id
	 * @return boolean
	 */
	public function deleteMe($id) {
		$count = $this->db->executeQuery("DELETE FROM web_plugin WHERE id = {$id}");
		return $count > 0 ? true : false;
	}

	/**
	 * 更新或添加插件
	 * @param string $plugin_name
	 * @param string $class_name
	 * @param string $author
	 * @param string $author_url
	 * @param string $description
	 * @param string $instruction
	 * @param int $instruction_type
	 * @param string $version
	 * @param int $is_monitor_all_msg
	 * @param int $is_able
	 */
	public function replaceMe($plugin_name, $class_name, $author, $author_url, $description, $instruction, $instruction_type, $version, $is_monitor_all_msg, $is_able) {
		$count = $this->db->executeQuery("REPLACE INTO web_plugin(plugin_name, class_name, author, author_url, description, instruction, instruction_type, version, is_monitor_all_msg, is_able) VALUES('{$plugin_name}', '{$class_name}', '{$author}', '{$author_url}', '{$description}', '{$instruction}', {$instruction_type}, '{$version}', {$is_monitor_all_msg}, {$is_able})");
		return $count > 0 ? true : false;
	}

	/**
	 * 修改插件指令、监控消息类型和启用状态
	 * @param int $id
	 * @param string $instruction
	 * @param int $is_monitor_all_msg
	 * @param int $is_able
	 * @return boolean
	 */
	public function updateMe($id, $instruction, $is_monitor_all_msg, $is_able) {
		return $this->db->executeQuery("UPDATE web_plugin SET instruction = '{$instruction}', is_monitor_all_msg = {$is_monitor_all_msg}, is_able = {$is_able} WHERE id = {$id}") > 0 ? true : false;
	}

	/**
	 * 执行插件安装的SQL
	 * @param string $sql
	 * @return boolean
	 */
	public function installSql($sql) {
		if (DataUtil::is_empty($sql)) return true;
		return $this->db->executeMultiQuery($sql, false);
	}

}
?>